﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MutilLanguage_Search.Controller;
using MutilLanguage_Search.Data;
using System.Windows.Forms;
using System.Net;
using HtmlAgilityPack;
using System.Text.RegularExpressions;
using System.Web;
using MutilLanguage_Search.Define;
namespace MutilLanguage_Search.Controller.GetLinks
{

    class GetLinksSynonyms
    {
        public static ListBox lbxLinks;
        public static void Init(string Key)
        {
            string link = "https://www.google.com/search?q=" + Key + "&num=10";
            WebClient webClient; webClient = new WebClient();
            webClient.DownloadDataCompleted += new DownloadDataCompletedEventHandler(webClient_DownloadDataCompleted);
            webClient.DownloadDataAsync(new Uri(link));
        }
        static void webClient_DownloadDataCompleted(object sender, DownloadDataCompletedEventArgs e)
        {
            try
            {
                HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
                doc.LoadHtml(ASCIIEncoding.UTF8.GetString(e.Result));
                SourceAnalysis.ParseGoogeSyn(doc);
            }
            catch
            {
            }
            finally
            {
                Variables.countSyn++;
                if (Variables.countSyn == Data.Init.sdSynonyms.Count)
                {
                    foreach (KeyValuePair<string, string> kv in Data.Init.sdLinksSyn)
                    {
                        string key = kv.Key;
                        GetKeyWords.GetContentAllLinksSyn.Init(kv.Key);
                        lbxLinks.Items.Add(key);
                    }
                }
            }
        }
    }
}
